package com.hsnn.medstgmini.trading.pur.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.exception.MSException;
import com.hsnn.medstgmini.model.Status;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.trading.pur.model.HospAddress;
import com.hsnn.medstgmini.trading.pur.model.PurConfig;
import com.hsnn.medstgmini.trading.pur.model.form.CreateOrderForm;
import com.hsnn.medstgmini.trading.pur.service.AddressManager;
import com.hsnn.medstgmini.trading.pur.service.CreateOrderManager;
import com.hsnn.medstgmini.trading.pur.service.PurConfigManager;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 医疗机构生成订单
 * @author 蔡春龙
 * @date 2015年7月8日
 */
@Controller
@RequestMapping("/createOrder")
public class CreateOrderController {

	private static final Logger log = Logger
			.getLogger(CreateOrderController.class);

	@Autowired
	private AddressManager addressManager;

	@Autowired
	private CreateOrderManager createOrderManager;
	
	@Autowired
	private PurConfigManager purConfigManager;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private HttpSession session;

	/**
	 * @category 获取当前登陆人信息
	 * @date 2015年6月14日
	 * @return
	 */
	public SysUser getUser() {
		return (SysUser) session.getAttribute(Constants.USERINFO);
	}

	/**
	 * @category 进入生成订单页面
	 * @date 2015年7月9日
	 * @return
	 */
	@RequestMapping("/toCreateOrder")
	public String toCreateOrder(Model model) {
		log.info("enter toCreateOrder");
		PurConfig purConfig = new PurConfig();
		HospAddress address = addressManager.getDefaultAddress(getUser());
		purConfig = purConfigManager.getPurConfigById(Constants.COMP_PS_END_DAY_NUM_ID);
		model.addAttribute("address", address);
		model.addAttribute("configValue",purConfig.getConfigValue());
		return "/trading/pur/createOrder";
	}

	/**
	 * @category 进入地址选择列表
	 * @date 2015年7月9日
	 * @return
	 */
	@RequestMapping("/toCreateOrderAddress")
	public String toCreateOrderAddress(Model model, String id) {
		log.info("进入地址选择列表id:"+id);
		model.addAttribute("id", id);
		return "/trading/pur/createOrderAddress";
	}

	/**
	 * @category 获取有效地址列表
	 * @date 2015年7月9日
	 * @param address
	 * @return
	 */
	@RequestMapping("/getCreateOrderAddress")
	@ResponseBody
	public Pagination getCreateOrderAddress(HospAddress address) {
		log.info("获取有效地址列表"+address);
		Pagination page = new Pagination(request);
		try {
			SysUser user = getUser();
			address.setStatus(Status.enabled.getKey());
			addressManager.getAddressList(address, page, user);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 提交订单
	 * @date 2015年7月10日
	 * @param orderForm
	 * @return
	 */
	@RequestMapping("/submitOrder")
	@ResponseBody
	public Pagination submitOrder(CreateOrderForm orderForm) {
		log.info("提交订单 "+orderForm);
		Pagination page = new Pagination(request);
		try {
			if (orderForm != null) {
				createOrderManager.submitOrder(orderForm, getUser());
				page.setSuccess(true);
			}
		} catch (MSException e) {
			log.error("",e);
			page.setSuccess(false);
			page.setCode(e.getCode());
			page.setMsg(e.getMsg());
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

}
